<template >
  <div class="container">
    <el-container class="home-container">
      <el-header>
        <div class="left">
          <i class="el-icon-s-tools"></i>
          <span> 电商后台管理系统 </span>
        </div>
        <el-button type="info" @click="logout"> 退出登录 </el-button>
      </el-header>
      <el-container>
        <el-aside :width="iscollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapce">＜＞</div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#e682cd"
            text-color="#fff"
            active-text-color="#13e296"
            unique-opened
            :collapse="iscollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <el-submenu
              v-for="item in menuList"
              :key="item.id"
              :index="item.id + ''"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + second.path"
                v-for="second in item.children"
                :key="second.id"
                @click="saveNavState('/' + second.path)"
              >
                <template slott="title">
                  <i class="el-icon-menu"></i>
                  {{ second.authName }}
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'el-icon-user',
        103: 'el-icon-setting',
        101: 'el-icon-goods',
        102: 'el-icon-s-claim',
        145: 'el-icon-loading',
      },
      iscollapse: false,
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      let { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
    },
    toggleCollapce() {
      this.iscollapse = !this.iscollapse
    },
    saveNavState(active) {
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    },
  },
}
</script>
<style lang="less" scoped>
.container {
  height: 100%;
}
.home-container {
  height: 100%;
}
.el-header {
  align-items: center;
  background-color: #e2a4d3;
  display: flex;
  justify-content: space-between;
  .left {
    color: cyan;
    font-size: 30px;
    line-height: 60px;
  }
}
.el-aside {
  .toggle-button {
    text-align: right;
    color: #fff;
    cursor: pointer;
    border-top: 1px dashed #ccc;
  }
  color: #fff;
  background-color: #e2a4d3;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eee;
}
</style>